//文件上传api
import Busboy from 'busboy'

export default defineEventHandler(async (event:any) => {
  const busboy = Busboy({ headers: event.req.headers })
  const files:any = {};
  const fields:any = {};

await new Promise((resolve, reject) => {
    event.req.pipe(busboy);
    busboy.on('file', (fieldname, file, filename) => {
      console.log('File [' + fieldname + ']: filename: ' + filename);
      files[fieldname] = file;
    });
    busboy.on('field', (fieldname, value) => {
      console.log('Field [' + fieldname + ']: value: ' + value);
      fields[fieldname] = value;
    });
    busboy.on('finish', () => {
      resolve('success');
    });
    busboy.on('error', (err) => {
      reject(err);
    });
  });

  const file = files['file']; // 确保字段名匹配客户端代码
  console.log(file);

  return {
    status: 200,
    data: {
      file:file.File,
      filename:file.filename
    }
  }
});